/// <reference path="sage.util.js" />
/// <reference path="sage.input.__.base.js" />

var TapInput = BaseInputHandler.extend({
    initialize: function initialize() {
        BaseInputHandler.initialize.call(this, ["vclick.touch-input", "tap.touch-input", "taphold.touch-input", "swipe.touch-input"]);
    },
    sendVClick: function sendVClick(context) {
        for (subId in this.subscribers) {
            this.subscribers[subId].fireEvent("vclick", context);
        }
    },
    sendTap: function sendTap(context) {
        for (subId in this.subscribers) {
            this.subscribers[subId].fireEvent("tap", context);
        }
    },
    sendTapHold: function sendTapHold(context) {
        for (subId in this.subscribers) {
            this.subscribers[subId].fireEvent("taphold", context);
        }
    },
    sendSwipe: function sendSwipe(context) {
        for (subId in this.subscribers) {
            this.subscribers[subId].fireEvent("swipe", context);
        }
    },
    setup: function setup() {
        if (!this.enabled) {
            this.enabled = true;
            var self = this;
            $(window).on('vclick.touch-input', function (event) {
                var context = { prevent: false, clientX: event.clientX, clientY: event.clientY };
                self.sendVClick(context);
                if (context.prevent) event.preventDefault();
            });
            $(window).on('tap.touch-input', function (event) {
                var context = { prevent: false, event: event };
                self.sendVClick(context);
                if (context.prevent) event.preventDefault();
            });
            $(window).on('taphold.touch-input', function (event) {
                var context = { prevent: false, event: event };
                self.sendVClick(context);
                if (context.prevent) event.preventDefault();
            });
            $(window).on('swipe.touch-input', function (event) {
                var context = { prevent: false, event: event };
                self.sendVClick(context);
                if (context.prevent) event.preventDefault();
            });
        }
    }
});
